package mdrtu;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.WriteCoilsRequest;
import com.serotonin.modbus4j.msg.WriteCoilsResponse;
import com.serotonin.modbus4j.msg.WriteRegistersRequest;
import com.serotonin.modbus4j.msg.WriteRegistersResponse;

public class registers_6 {
	public static String commPortId;
	public static int baudRate; 
	public static int dataBits;
	public static int stopBits;
	public static int parity;
	public static int slaveId;
	public static int start;
	public static short[] values;
	public registers_6(String commPortId1, int baudRate1, int dataBits1, int stopBits1, int parity1, int slaveId1, int start1, short[] values1)
	{
		 commPortId = commPortId1; 
		 baudRate = baudRate1; 
		 dataBits = dataBits1;
		 stopBits = stopBits1;
		 parity = parity1;
		 slaveId = slaveId1;
		 start = start1;
		 values  = values1;
	};
	public static int remain1(){
		int result;
		SerialParameters serialParameters = serialParaSet(commPortId, baudRate, dataBits,  stopBits,  parity);
		ModbusFactory modbusFactory = new ModbusFactory();

        ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        result = writeRegistersTest(master, slaveId, start, values);
        master.destroy();
        return result;
	};
	private static SerialParameters serialParaSet(String commPortId3, int baudRate3, int dataBits3, int stopBits3, int parity3) {
		SerialParameters serialParameters = new SerialParameters();		
		serialParameters.setCommPortId(commPortId3);//璁惧畾MODBUS閫氳鐨勪覆琛屽彛		
		serialParameters.setParity(parity3);//璁惧畾鎴愭棤濂囧伓鏍￠獙		
		serialParameters.setDataBits(dataBits3);//璁惧畾鎴愭暟鎹綅鏄�8浣�		
		serialParameters.setStopBits(stopBits3);//璁惧畾涓�1涓仠姝綅
		serialParameters.setPortOwnerName("Numb nuts");		
		serialParameters.setBaudRate(baudRate3);//涓茶鍙ｄ笂鐨勬尝鐗圭巼
		return serialParameters;
	}
	 public static int writeRegistersTest(ModbusMaster master, int slaveId3, int start3, short[] values3) {
	     int result1 = 0;   
		 try {
	            WriteRegistersRequest request = new WriteRegistersRequest(slaveId3, start3, values3);
	            WriteRegistersResponse response = (WriteRegistersResponse) master.send(request);

	            if (response.isException())
	               {
	            	System.out.println("Exception response: message=" + response.getExceptionMessage());
	               result1 = 0;
	               }
	            else
	                {
	            	System.out.println("Success");
	                result1 = 1;
	                }
	        }
	        catch (ModbusTransportException e) {
	            e.printStackTrace();
	        }
		 return result1;
	    }

}
